package com.qs.eggs.wxapi;

import com.google.gson.Gson;
import com.qs.eggs.R;
import com.qs.eggs.view.activity.PayMethodActivity;
import com.qs.eggs.view.activity.SubmitOrderActivity;
import com.qs.eggs.view.ordermanager.activity.WaitSendActivity;
import com.qs.eggs.view.prodetail.activity.ProDetailActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {

		//errCode为：0表示成功 -1表示错误 -2表示用户取消
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);
			if(resp.errCode==0){
				Toast.makeText(this,"支付成功",Toast.LENGTH_SHORT).show();
				Intent intent=new Intent(WXPayEntryActivity.this, WaitSendActivity.class);
				startActivity(intent);
				WXPayEntryActivity.this.finish();
				WXPayActivity.instance.finish();
				if (PayMethodActivity.instance!=null){
					PayMethodActivity.instance.finish();
				}
				if (SubmitOrderActivity.instance!=null){
					SubmitOrderActivity.instance.finish();
				}
				if (ProDetailActivity.instance!=null){
					ProDetailActivity.instance.finish();
				}

			}else if(resp.errCode==-1){
				finish();
				WXPayActivity.instance.finish();
				Toast.makeText(this,"支付失败",Toast.LENGTH_SHORT).show();
			}else if(resp.errCode==-2){
				finish();
				WXPayActivity.instance.finish();
				Toast.makeText(this,"支付取消",Toast.LENGTH_SHORT).show();
			}
		}
	}


}